package com.qzxy.premiumhotelsystem.config;

import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.filter.SaServletFilter;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.jwt.StpLogicJwtForSimple;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.strategy.SaStrategy;
import com.qzxy.premiumhotelsystem.common.constant.UserRoleConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.lang.reflect.AnnotatedElement;
import java.util.List;

/**
 * @author 李佳添
 * @version 1.0
 * @description
 * @date 2024/9/17 9:41 周二
 */

@Configuration
public class SaTokenConfig implements WebMvcConfigurer {

    @Bean
    public StpLogicJwtForSimple getStpLogicJwt() {
        return new StpLogicJwtForSimple();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(new SaInterceptor(handler -> {
            SaRouter.match("/file/**", r -> System.out.println("----是否登录----"))
                    .check(r -> StpUtil.checkLogin());

            SaRouter.match("/guests/**", r -> System.out.println("----顾客权限----"))
                    .notMatch("/guests/verification/**")
                    .notMatch("/guests/forger-password/**")
                    .notMatch("/guests/manage/**")
                    .check(r -> StpUtil.checkRoleOr(UserRoleConstant.GUEST, UserRoleConstant.EMPLOYEE, UserRoleConstant.ADMIN));
            SaRouter.match("/guests/manage/**", r -> System.out.println("----员工权限_办理入住----"))
                    .check(r -> StpUtil.checkRoleOr(UserRoleConstant.EMPLOYEE, UserRoleConstant.ADMIN));
            SaRouter.match("/user/**",r -> System.out.println("----顾客权限----"))
                    .notMatch("/user/rooms/**")
                    .notMatch("/user/vip/types","/user/vip/vipId","/user/vip/all")
                    .check(r -> StpUtil.checkRoleOr(UserRoleConstant.GUEST, UserRoleConstant.EMPLOYEE, UserRoleConstant.ADMIN));


            SaRouter.match("/employees/**", r -> System.out.println("----员工权限----"))
                    .notMatch("/employees/verification/**")
                    .notMatch("/employees/forger-password/**")
                    .check(r -> StpUtil.checkRoleOr(UserRoleConstant.EMPLOYEE, UserRoleConstant.ADMIN));

            SaRouter.match("/admin/**", r -> System.out.println("----管理员权限----"))
                    .notMatch("/admin/verification/**")
                    .check(r -> StpUtil.checkRole(UserRoleConstant.ADMIN));
            SaRouter.match("/excel/**", r -> System.out.println("----管理员权限_数据导入导出模块----"))
                    .check(r -> StpUtil.checkRole(UserRoleConstant.ADMIN));
            SaRouter.match("/logs/**", r -> System.out.println("----管理员权限_日志模块----"))
                    .check(r -> StpUtil.checkRole(UserRoleConstant.ADMIN));

        })).addPathPatterns("/**");
        ;
    }

    public static final StpLogic adminStpLogic = SaManager.getStpLogic(UserRoleConstant.ADMIN);
    public static final StpLogic employeeStpLogic = SaManager.getStpLogic(UserRoleConstant.EMPLOYEE);
    public static final StpLogic guestStpLogic = SaManager.getStpLogic(UserRoleConstant.GUEST);

    @Autowired
    @SuppressWarnings("all")
    public void rewriteSaStrategy() {
//        重写Sa-Token的注解处理器，增加注解合并功能
        SaStrategy.instance.getAnnotation = (element, annotationClass) -> {
            return AnnotatedElementUtils.getMergedAnnotation(element, annotationClass);
        };
    }

//    @Bean
//    public SaServletFilter getSaServletFilter() {
//        return new SaServletFilter()
//                .setIncludeList(List.of("/**"))
//                .addExclude("/favicon.ico")
//                .setBeforeAuth(obj -> {
//                    System.out.println("SaTokenConfigure.getSaServletFilter.setBeforeAuth");
//                }).setAuth(obj -> {
//                    SaRouter.match("/**").check(() -> {
//                        System.out.println("鉴权");
//                    });
//                }).setError(err->{
//                    System.out.println("SaTokenConfigure.getSaServletFilter.setError");
//                    return "哎哟，你干嘛";
//                });
//    }
}

